package com.jeeplus.test.jlnu.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AdmissionInfo;
import com.jeeplus.test.jlnu.admissionInfo.mapper.AdmissionInfoMapper;
import com.jeeplus.test.jlnu.student.domain.result.Node;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "目录树")
@RestController
@Slf4j
@RequestMapping("/studentTree")
public class StduentTreeController {
    @Autowired
    private AdmissionInfoMapper admissionInfoMapper;

    private List<Node> convertToTargetFormat(Map<String, Map<String, List<String>>> data) {
        List<Node> result = new ArrayList<>();

        if (data == null || data.isEmpty()) {
            return result;
        }

        for (Map.Entry<String, Map<String, List<String>>> academyEntry : data.entrySet()) {
            String academyName = academyEntry.getKey();
            if (academyName == null || academyEntry.getValue() == null || academyEntry.getValue().isEmpty()) {
                continue;
            }

            Node academyNode = new Node(academyName);

            for (Map.Entry<String, List<String>> majorEntry : academyEntry.getValue().entrySet()) {
                String majorName = majorEntry.getKey();
                if (majorName == null || majorEntry.getValue() == null || majorEntry.getValue().isEmpty()) {
                    continue;
                }

                Node majorNode = new Node(majorName);

                for (String time : majorEntry.getValue()) {
                    if (time == null) {
                        continue;
                    }
                    Node timeNode = new Node(time);
                    majorNode.addChild(timeNode);
                }

                if (!majorNode.getChildren().isEmpty()) {
                    academyNode.addChild(majorNode);
                }
            }

            if (!academyNode.getChildren().isEmpty()) {
                result.add(academyNode);
            }
        }
        return result;
    }

    @ApiOperation("录取信息树")
    @PostMapping("admissionTree")
    public String buildDirectoryTree(@RequestParam String officeId) throws JsonProcessingException {
        Map<String, Map<String, List<String>>> tree = new LinkedHashMap<>();
        // 3. 根据院校ID执行查询
        List<AdmissionInfo> admissionInfos = admissionInfoMapper.selectByOfficeId(officeId);

        for (AdmissionInfo row : admissionInfos) {
            // 检查 row 是否为 null，防止对 null 进行方法调用
            if (row == null) {
                continue; // 如果 row 为 null，则跳过这次循环
            }

            // 安全地获取各个字段，防止空指针异常
            String academy = row.getAdmissionAcademy();
            String major = row.getAdmissionMajor();
            String time = row.getAdmissionTime() != null ? row.getAdmissionTime().toString() : null;

            // 如果任何一个字段为 null，则不处理该条记录
            if (academy != null && major != null && time != null) {
                tree.computeIfAbsent(academy, k -> new LinkedHashMap<>())
                        .computeIfAbsent(major, k -> new ArrayList<>())
                        .add(time);
            }
        }

    // 假设convertToTargetFormat方法可以处理空数据并将数据转换为目标格式
        List<Node> formattedTree = convertToTargetFormat(tree);

    // 返回JSON字符串
        return new ObjectMapper().writeValueAsString(formattedTree);
    }


}
